All Questions
Tagged with phpobject-oriented
262 questions
2votes
1answer
148views
Is widening parameter types in a PHP interface implementation a good practice?
I have an interface defined as follows: interface MyInterface { public function setMyMethod(int $a); public function getMyMethod(): int; } And I have a class that widens the parameter type: ...
2votes
1answer
173views
How is $this provided in PHP?
I got a fundamental question about the $this keyword in PHP and how it is defined. Pracitcally, I know what it is and what it does. But I am interested in how it is provided / implemented? For example,...
2votes
2answers
268views
To maintain SOLID, should data preparation, conversion, and pre-computation for purposes of saving an object, be separate from data persistence layer?
I am facing a common situation where I am saving some values into database from a business object. I am using a relational database and usually I only need to save a few items that are part of the ...
1vote
2answers
625views
Share data between users without a database, php [closed]
How would I create for example a live chat, where you "post" a piece of text and it is displayed to other users in a "chat room", without the need of permanently storing it. My ...
0votes
2answers
67views
Getters and (static) processor vs multiple processor wrappers
I've got a class which stores two data series like so. I need to do some complicated processing on each of these arrays, but for now we'll just get the average of each data series. I could either do ...
-1votes
1answer
695views
Adapter pattern for formatting third party API responses
I've been refactoring some of my procedural code to OOP, and I'm wondering if using the adapter pattern is overkill in this case. Basically, I created a Order class. All class properties are a field ...
2votes
4answers
540views
Would this violate the Liskov Substitution Principle?
Say I have a set of objects from an "old system" that I want to convert to a newer set of corresponding classes. Each specific class has its own way of being converted. So I have this: ...
1vote
1answer
143views
How to prevent mutual dependencies when implementing database relationships
To explain the problem imagine you have two entities User and Group. The OO implementation has two classes UserModel and GroupModel. UserModel should have a method getGroups() (returning instances of ...
1vote
1answer
271views
Is this correct adherence to Liskov Substitution Principle?
I used to have a base object with subtypes behaving in all but the same way -- the difference being in their render methods. This base class defined a default render method, overridden by Some ...
0votes
1answer
136views
Design leading to break contravariance. How to avoid it?
I have a Converter which use a Resolver to determine which Factory to use when converting a Resource to a Entity. To do so, I need to be sure that the Factory has a createFromResource method. The ...
0votes
2answers
112views
How can you avoid doing hundreds/thousands of queries when utilizing classes in PHP?
I have been dipping my toes into object orientated PHP programming and I'm finding it useful but I can't help but feel I'm missing something in regards to how to utilize it. Normally my classes and ...
0votes
3answers
167views
Should it be considered a BC break to return clone instead of new self?
(This question applies to the equivalent code in both Java and PHP) I have a class like this: class Foo { private int $bar; public function __construct(int $bar) { $this->bar = $...
1vote
1answer
260views
Could this be considered a valid State GoF Pattern implementation?
For teaching purpose, I would like to create a simple implementation of State Pattern using PHP 7.4. So, I've tried to create a simple "document state machine" starting with Draft, sending to review ...
1vote
1answer
219views
Can Value Objects create their own value?
Let's say you have a username value object which has formatting rules e.g., class Username { private $username; public function __construct(string $username) { // thrown an ...
2votes
2answers
3kviews
Builder Pattern: Is it acceptable to use "passing-by-reference" on Director methods?
For teaching purposes, I am trying to create a PHP implementation of a conceptual example of Builder Pattern: First of all, some products: class Product1 { private string $attribute1; ...